package edu.uj.petri.controller;

import edu.uj.petri.document.Document;
import edu.uj.petri.model.Transition;
import edu.uj.petri.simulation.Simulation;
import edu.uj.petri.simulation.SimulationException;
import edu.uj.petri.view.DiagramElement;
import edu.uj.petri.view.TransitionView;
import java.awt.geom.Point2D;

public class SimulationEventHandler implements EventHandler {
    public SimulationEventHandler() {
        
        if (NewInhibitorArcEventHandler.unfinished != null) 
        {
            Document.getActiveDocument().getView().removeElement(NewInhibitorArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewInhibitorArcEventHandler.unfinished = null;
            
        } else if (NewNormalArcEventHandler.unfinished != null)  
        {
            Document.getActiveDocument().getView().removeElement(NewNormalArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewNormalArcEventHandler.unfinished = null;
        }
    }

    public void click(double x, double y) {
        Document document = Document.getActiveDocument();
        DiagramElement e = document.getView().getClickedElement( new Point2D.Double( x, y ), true);
        if (e instanceof TransitionView) {
            Transition transition = (Transition)((TransitionView)e).getNode();
            if (!transition.canBeFired())
                return;
            Simulation simulation = document.getSimulation();
            try {
                simulation.fireTransition(transition);
            } catch (SimulationException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void doubleClick(double x, double y) {
    }

    public void mouseMove(double x1, double y1, int button) {
    }

    public void mouseButtonDown(double x, double y) {
    }

    public void mouseButtonUp(double x, double y) {
    }

    public void rightClick(double x, double y) {
    }

    /**
     * handles Escape and Delete
     */
    public void keyPressed(int keyCode) {
    }

    public void mouseDragged(double x, double y) {
    }

}
